var items = new Array();
$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmb/saveCmsmb.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$("#checkalladd").click(function() {
		if($(this).attr("checked") == "checked") {
			$("[name='meaaAdd']").attr("checked", 'true');
		} else {
			$("[name='meaaAdd']").removeAttr("checked");
		}
	});
	$('#updateForm').submit(function() {
		$.ajax({
			url: '/wms/cmsmb/updateCmsmb.xhtml',
			data: $('#updateForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("修改成功!");
					closeDialogUpdate();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	$("#checkallupdate").click(function() {
		if($(this).attr("checked") == "checked") {
			$("[name='meaaUpdate']").attr("checked", 'true');
		} else {
			$("[name='meaaUpdate']").removeAttr("checked");
		}
	});
	ajaxTable();
	setDialogSearch();
	closeDialogSearch();
	setDialogAdd();
	closeDialogAdd();
	setDialogUpdate();
	closeDialogUpdate();
	if(globalMaps.get('2') == undefined) {
		$("#toolbarAdd").hide();
	}
	if(globalMaps.get('4') == undefined) {
		$("#toolbarDelete").hide();
	}
});
function ajaxTable() {
	$('#cmsmbTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				openDialogAdd();
			}
		}, '-', {
			id : 'toolbarDelete',
			text : '删除',
			iconCls : 'icon-cancel',
			handler : function() {
				batch('delete');
			}
		}, '-', {
			id : 'toolbarSearch',
			text : '查找',
			iconCls : 'icon-search',
			handler : function() {
				openDialogSearch();
			}
		} ],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
			mbaa : $("#searchMbaa").val(),
			mbab : $("#searchMbab").val()
		},
		onClickRow : function(rowIndex, rowData) {
			$('#cmsmbTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			if(globalMaps.get('3') != undefined) {
				openDialogUpdate(rowData.mbaa,rowData.mbab,rowData.mbac);
			}
	    }
	}).datagrid('acceptChanges');
}
function reloadTable() {
	$('#cmsmbTable').datagrid('reload');
	$('#cmsmbTable').datagrid('clearSelections');
}
function setDialogSearch() {
	$('#cmsmbSearch').dialog({
		title : '查询部门',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogSearch() {
	$('#cmsmbSearch').dialog('open');
}
function closeDialogSearch() {
	$('#cmsmbSearch').dialog('close');
}
function searchCmsmb() {
	var queryParams = $('#cmsmbTable').datagrid('options').queryParams;
	queryParams.mbaa = $("#searchMbaa").val();
	queryParams.mbab = $("#searchMbab").val();
	reloadTable();
	closeDialogSearch();
}
function setDialogAdd() {
	$('#cmsmbAdd').dialog({
		title : '新增部门',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogAdd() {
	cmsmbAddReset();
	$('#cmsmbAdd').dialog('open');
}
function closeDialogAdd() {
	$('#cmsmbAdd').dialog('close');
}
function cmsmbAdd() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
function cmsmbAddReset() {
	$("#cmsmbAddMbaa").val("");
	$("#cmsmbAddMbab").val("");
	$("#cmsmbAddMbac").val("");
	$("[name='meaaAdd']").removeAttr("checked");
	$("#checkalladd").removeAttr("checked");
}
function setDialogUpdate() {
	$('#cmsmbUpdate').dialog({
		title : '修改部门',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogUpdate(mbaa, mbab, mbac) {
	items[0] = mbaa;
	items[1] = mbab;
	items[2] = mbac;
	cmsmbUpdateReset(mbaa, mbab, mbac);
	$('#cmsmbUpdate').dialog('open');
}
function closeDialogUpdate() {
	$('#cmsmbUpdate').dialog('close');
}
function cmsmbUpdateReset(mbaa, mbab, mbac) {
	var roleMapArray = new Array();
	var url = "/wms/cmsmg/cmsmgList.xhtml?mbaa="+mbaa+"&callback=?";
	jQuery.getJSON(url, null, function call(result){   
		jQuery.each(result.data, function(i, obj) {
			var mgac = obj.mgac;
			roleMapArray[i] = mgac;
		});
		$("#cmsmbInfoUpdateMbaa").val(mbaa);
		$("#cmsmbUpdateMbaa").text(mbaa);
		$("#cmsmbUpdateMbab").val(mbab);
		$("#cmsmbUpdateMbac").val(mbac);
		$("#checkallupdate").removeAttr("checked");
		$("input[name=meaaUpdate]").removeAttr("checked");
		$("input[name=meaaUpdate]").each(function() {
			var checkBox = $(this);
			$.each(roleMapArray, function(n, value) {
				if(checkBox.val() == value) {
					checkBox.attr("checked", 'true');
				}
			});
		});
	});
}
function cmsmbUpdate() {
	var validateResult = $("#updateForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#updateForm').submit();
}
function batch(flag) {
	if ($('#cmsmbTable').datagrid('getSelected')) {
		var ids = [];
		var cods = [];
		var selectedRow = $('#cmsmbTable').datagrid('getSelections');
		for ( var i = 0; i < selectedRow.length; i++) {
			ids.push(selectedRow[i].mbaa);
			cods.push(selectedRow[i].mbab);
		}
		var mbaa = ids.join(',');
		if(flag == "delete") {
			$.messager.confirm('删除提示', '你确定永久删除下列部门吗?<br/>' + cods.join(','), function(r) {
				if(r) {
					changeStatus('/wms/cmsmb/deleteCmsmbBatch.xhtml?mbaa=' + mbaa);
				}
			});
		}
		$('#cmsmbTable').datagrid('clearSelections');
	}
}
function changeStatus(url){
	$.ajax({
		async : false,
		cache : false,
		type : 'POST',
		dataType : "text",
		url : url,
		error : function () {
			alert('请求失败');
		},
		success : function(result){
			if(result=="success") {
				alert("操作成功!");
			} else {
				alert(result);
			}
			reloadTable();
		}
	});
}